package cn.bby.tools.utils;

import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import static cn.bby.tools.utils.StringImageUtil.*;


/**
 * @author ben
 * @since 2025/10/18 02:18
 */
public class StringImageUtilTest {
    public static final String DEFAULT_IMAGE_FORMAT = "PNG";
    public static final String DEFAULT_OUTPUT_DIR = "doc";

    @Test
    public void test() throws IOException {
        String text = "Hello, World!";
//        for (int i = 0; i <1000; i++) {
//            text += " Hello, World!";
//        }

        // 保存图像
        String fileName = "zxing_string_image_" + UUID.randomUUID() + "." + DEFAULT_IMAGE_FORMAT.toLowerCase();
        File imgFile = new File(DEFAULT_OUTPUT_DIR, fileName);
        ImageIO.write(encode(text), DEFAULT_IMAGE_FORMAT, imgFile);
        String decodedText = decodeMultipleToList(imgFile).toString();
        System.out.println("Decoded Text: " + decodedText);

    }

    @Test
    public void test2() {
        File file = new File(DEFAULT_OUTPUT_DIR, "img.png");
        if (file.exists()) {
            String decodedText = decodeSingle(file);
            System.out.println("Decoded Text: " + decodedText);
        } else {
            System.out.println("Test image file does not exist: " + file.getAbsolutePath());
        }
    }

    /**
     * 测试长字符串分割生成多个二维码的功能
     */
    @Test
    public void testLongStringToMultipleQRImages() {
        // 创建一个超过1000字符的长字符串
        StringBuilder longText = new StringBuilder();
        for (int i = 0; i < 1500; i++) {
            longText.append("字符").append(i).append(" ");
        }

        // 生成多个二维码
        List<BufferedImage> imageFiles = encodeMultiple(longText.toString());
        System.out.println("Generated " + imageFiles.size() + " QR images");
        File file = new File(DEFAULT_OUTPUT_DIR, "img.png");
        if (file.exists()) {
            String decodedText = decodeMultipleToList(file).toString();
            System.out.println("Decoded Text: " + decodedText);
        } else {
            System.out.println("Test image file does not exist: " + file.getAbsolutePath());
        }

    }
}